package com.funo.business.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.funo.business.dao.ProductDao;
import com.funo.business.entities.Product;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.tools.StringTool;
import com.funo.core.base.BaseDao;

@Repository("productDao")
public class ProductDaoImpl extends BaseDao<Product, String> implements ProductDao{
	/**
	 * 设置查询条件，做分页
	 * @param product
	 * @return
	 */
	private Criteria getCriteria(Product product){
		Criteria criteria = super.getCriteria();
		if (product!=null ) {
			if (StringTool.checkEmpty(product.getProductName())) { //产品名称
				criteria.add(Restrictions.eq("productName", product.getProductName()));
			}
			if (product.getProductItem()!=null ) { //产品类型
				criteria.add(Restrictions.eq("productItem", product.getProductItem()));
			}
			criteria.addOrder(Order.desc("productName"));
		}
		return criteria;
	}

	@Override
	public List<Product> pageList(QueryCondition condition,Product product) {
		Criteria criteria = getCriteria(product);
		criteria.setFirstResult(condition.getStart());
		criteria.setMaxResults(condition.getLimit());
		return super.findByCriteria(criteria);
	}

	@Override
	public Integer count(QueryCondition condition,Product product) {
		Criteria criteria = getCriteria(product);
		return super.countByCriteria(criteria);
	}

}
